/*
 * @Author: CodeHare
 * @Date: 2023-11-21 14:54:20
 * @LastEditors: CodeHare
 * @LastEditTime: 2023-11-22 14:53:33
 * @Description: 我的报障单单据详情
 */
import 'package:get/get.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/model/user_model.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/task/task_services.dart';
import 'package:yt_devops/pages/tasks/task_list/task_list_controller.dart';
import 'package:yt_devops/pages/tasks/my_fault_report/fault_report_detail/fault_report_detail_model.dart';

class FaultReportDetailController extends GetxController {
  TaskListController taskListController = Get.find();
  FaultReportDetailModel model = FaultReportDetailModel();
  final TaskServices services = Get.find();
  UserModel? selectUser;

  @override
  void onInit() {
    super.onInit();
    var id = Get.arguments['id'];
    // var id = 668;
    loadDetail(id);
  }

  @override
  void onReady() {}

  @override
  void onClose() {}

  loadDetail(int id) => Configs.httpClient.requestWithLoading(() async {
        var params = {'faultId': id};
        var data = await services.getReportTaskDetail(params);
        model = FaultReportDetailModel.fromJson(data);
        update();
      });

  accept(id, TaskType type) => Configs.httpClient.requestWithLoading(() async {
        await services.acceptReportTask({'id': id});
        BrnToast.show(
          ResKey.taskProcessSuccessTip.tr,
          Get.context!,
          preIcon: Image.asset(
            ImageAssets.iconSuccess,
            width: 24,
            height: 24,
          ),
          duration: BrnDuration.short,
          onDismiss: () {
            loadDetail(id);
            taskListController.refreshReport();
          },
        );
      });

  toFinish(id, TaskType type, ReportTaskStatusType statusType) {
    Get.toNamed(RouterPath.finishTask,
            arguments: {'id': id, 'status': statusType.value, 'type': type})
        ?.then((value) {
      if (value == true) {
        loadDetail(id);
        taskListController.refreshReport();
      }
    });
  }

  toDispatchReceiever(int id) => Configs.httpClient.requestWithLoading(
        () async {
          var data = await services.acceptReportTask({
            "id": model.id, // 工单id
            "handlerBy": selectUser?.userId, // 接收入
          });
          BrnToast.show(
            '${ResKey.submitSuccessTip.tr}! ',
            Get.context!,
            preIcon: Image.asset(
              ImageAssets.iconSuccess,
              width: 24,
              height: 24,
            ),
            duration: BrnDuration.short,
            onDismiss: () {
              loadDetail(id);
              taskListController.refreshReport();
            },
          );
        },
      );

  void toInstaller() {
    // Get.toNamed(RouterPath.maintainerList,
    //     arguments: {'siteID': model.id, 'user': selectUser})?.then((value) {
    //   if (value is UserModel) {
    //     selectUser = value;
    //     update();
    //   }
    // });
  }
}
